package com.acdc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.acdc.domain.IotModule;
import com.acdc.domain.IotModuleHeartbeat;
import com.acdc.mqtt.service.IotModuleSender;
import com.acdc.service.IIotModuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物联网模块Controller
 * 
 * @author acdc
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/device/iot")
public class IotModuleController extends BaseController
{
    @Autowired
    private IIotModuleService iotModuleService;

    @Autowired
    private IotModuleSender iotModuleSender;

    /**
     * 查询物联网模块列表
     */
    @PreAuthorize("@ss.hasPermi('device:iot:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotModule iotModule)
    {
        startPage();
        List<IotModule> list = iotModuleService.selectIotModuleList(iotModule);
        return getDataTable(list);
    }

    /**
     * 导出物联网模块列表
     */
    @PreAuthorize("@ss.hasPermi('device:iot:export')")
    @Log(title = "物联网模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotModule iotModule)
    {
        List<IotModule> list = iotModuleService.selectIotModuleList(iotModule);
        ExcelUtil<IotModule> util = new ExcelUtil<IotModule>(IotModule.class);
        util.exportExcel(response, list, "物联网模块数据");
    }

    /**
     * 获取物联网模块详细信息
     */
    //@PreAuthorize("@ss.hasPermi('device:iot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(iotModuleService.selectIotModuleById(id));
    }

    /**
     * 新增物联网模块
     */
    @PreAuthorize("@ss.hasPermi('device:iot:add')")
    @Log(title = "物联网模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotModule iotModule)
    {
        return toAjax(iotModuleService.insertIotModule(iotModule));
    }

    /**
     * 修改物联网模块
     */
    @PreAuthorize("@ss.hasPermi('device:iot:edit')")
    @Log(title = "物联网模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotModule iotModule)
    {
        return toAjax(iotModuleService.updateIotModule(iotModule));
    }

    /**
     * 删除物联网模块
     */
    @PreAuthorize("@ss.hasPermi('device:iot:remove')")
    @Log(title = "物联网模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotModuleService.deleteIotModuleByIds(ids));
    }


    /**
     * 更新OTA
     * 
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('device:iot:op')")
    @Log(title = "物联网模块", businessType = BusinessType.UPDATE_OTA)
    @PostMapping("/updateOTA/{imei}")
    public AjaxResult updateOTA(@PathVariable String imei) {
        iotModuleSender.openOTA(imei);
        return success();
    }

    /**
     * 设置IOT模块时间
     * 
     * @param imei
     * @return
     */
    @PreAuthorize("@ss.hasPermi('device:iot:op')")
    @Log(title = "物联网模块", businessType = BusinessType.UPDATE_TIME)
    @PostMapping("/setTime/{imei}")
    public AjaxResult setTime(@PathVariable String imei) {
        iotModuleSender.setTime(imei);
        return success();
    }

    /**
     * 设置IOT模块心跳时间
     * 
     * @param imei
     * @return
     */
    @PreAuthorize("@ss.hasPermi('device:iot:op')")
    @Log(title = "物联网模块", businessType = BusinessType.UPDATE_HEARTUP_TIME)
    @PostMapping("/setHeartupTime/{imei}")
    public AjaxResult setHeartupTime(@PathVariable String imei, @RequestParam Integer timeout) {
        iotModuleSender.setHeartupTime(imei, timeout);
        return success();
    }

    /**
     * 设置IOT模块开关状态
     * 
     * @param imei
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('device:iot:op')")
    @Log(title = "物联网模块", businessType = BusinessType.UPDATE_PWR_STA)
    @PostMapping("/setPwrSta/{imei}")
    public AjaxResult setPwrSta(@PathVariable String imei, @RequestParam Integer pwrSta) {
        iotModuleSender.setPwrSta(imei, pwrSta);
        return success();
    }

    /**
     * 查询物联网模块心跳记录
     * 
     * @param iotModuleId 物联网模块ID
     * @return 物联网模块心跳记录
     */
    @PreAuthorize("@ss.hasPermi('device:iot:query')")
    @GetMapping("/heartbeat")
    public TableDataInfo heartbeat(IotModuleHeartbeat iotModuleHeartbeat)
    {
        startPage();
        List<IotModuleHeartbeat> list = iotModuleService.selectIotModuleHeartbeatList(iotModuleHeartbeat);
        return getDataTable(list);
    }
}
